import 'package:boss/models/commodity.dart';
import 'package:boss/models/store_menu.dart';
import 'package:flutter/material.dart';
import 'package:lpinyin/lpinyin.dart';

class SearchCommodityProvider extends ChangeNotifier {
  List<StoreMenu> menus = [];
  String searchWord = '';

  //当前所有的菜品列表
  List<Commodity> get commoditys {
    List<Commodity> list = [];

    this.menus.forEach((menu) {
      list.addAll(menu.commodityList);
    });

    return list;
  }

  //搜索匹配到的菜品
  List<Commodity> get searchCommoditys {
    if (searchWord == '') {
      return [];
    }

    return commoditys.where((commodity) {
      if (commodity.title.contains(searchWord)) return true;

      var shortTitle = PinyinHelper.getShortPinyin(commodity.title);
      if (shortTitle.contains(searchWord.toLowerCase())) {
        return true;
      }

      return false;
    }).toList();
  }

  SearchCommodityProvider(this.menus);

  //修改搜索关键字
  setSearchWord(String keyword) {
    searchWord = keyword;
    notifyListeners();
  }
}
